package org.xu.utils;

import org.springframework.util.StringUtils;
import org.xu.bo.PicCellBaseBO;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

public class GenerateImageUtil {

     public static BufferedImage generateImage(PicCellBaseBO cellBaseBO, List<List<String>> dataList){
         int imageWidth = cellBaseBO.getImageWidth();
         int imageHeight = cellBaseBO.getImageHeight();
         List<Integer> colWidthList = cellBaseBO.getColWidthList();
         // 字体大小
         int fontTitleSize = cellBaseBO.getFontTitleSize();
         // 横线的行数
         int totalRow = dataList.size() + 1;
         // 竖线的行数
         int totalCol = dataList.size();
         // 行高
         int rowHeight = cellBaseBO.getRowHeight();
         // 单元格宽度
         int totalCellWith = colWidthList.stream().reduce(Integer::sum).get();
         int totalCellHeight = totalCol * rowHeight;
         // 起始宽度
         int startWidth = (imageWidth - totalCellWith) / 2;
         // 起始高度
         int startHeight = imageHeight / 2 - (totalCellHeight) / 2;
         BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
         Graphics graphics = image.getGraphics();
         graphics.setColor(Color.WHITE);
         graphics.fillRect(0, 0, imageWidth, imageHeight);
         graphics.setColor(new Color(220, 240, 240));
         //画横线
         for (int j = 0; j < dataList.size() + 1 ; j++) {
             graphics.setColor(Color.black);
             int x2 = (imageWidth + totalCellWith) / 2;
             int y1 = startHeight + j * rowHeight;
             int y2 = startHeight + j * rowHeight;
             graphics.drawLine(startWidth, y1, x2, y2);
         }
         for (int i = 0; i < colWidthList.size(); i++) {
             int colWidth2 = 0;
             if (i > 0){
                 colWidth2 = colWidthList.subList(0,i).stream().reduce(Integer::sum).get();
             }
             int x = startWidth + colWidth2;
             int y11 = startHeight + rowHeight * (totalRow - 1);
             graphics.drawLine(x, startHeight, x, y11);
             if (i == colWidthList.size() -1){
                 int lastWidth = colWidthList.get(i);
                 colWidth2 = colWidthList.subList(0,i).stream().reduce(Integer::sum).get();
                 x = startWidth + colWidth2 + lastWidth;
                 graphics.drawLine(x, startHeight, x, y11);
             }
         }
         //设置字体
         String FONT = cellBaseBO.getFont();
         Font font;
         for (int n = 0; n < dataList.size(); n++) {
             List<String> cellBOS = dataList.get(n);
             for (int l = 0; l < cellBOS.size(); l++) {
                 if (n == 0) {
                     font = new Font(FONT, Font.BOLD, fontTitleSize);
                     graphics.setFont(font);
                     graphics.setColor(Color.BLACK);
                 } else if (l > 0) {
                     font = new Font(FONT, Font.PLAIN, fontTitleSize);
                     graphics.setFont(font);
                     graphics.setColor(cellBaseBO.getContentColor());
                 } else {
                     font = new Font(FONT, Font.PLAIN, fontTitleSize);
                     graphics.setFont(font);
                     graphics.setColor(cellBaseBO.getContentColor());
                 }
                 //向单元格里写内容，水平居中&垂直居中
                 FontMetrics fontMetrics = graphics.getFontMetrics(font);
                 String content = cellBOS.get(l);
                 //获取写入内容的宽度
                 int contentWidth = fontMetrics.stringWidth(content);
                 //获取字体的高度
                 int contentHeight = fontMetrics.getHeight();
                 //获取字体的下降值
                 int contentDescent = fontMetrics.getDescent();
                 //计算x、y坐标
                 int colWidth = colWidthList.get(l);
                 int xx = 0;
                 if (l == 0){
                     xx = startWidth + (colWidth - contentWidth) / 2;
                 }else {
                     int sumOffset = colWidthList.subList(0, l).stream().reduce(Integer::sum).get();
                     xx = startWidth + sumOffset + (colWidth - contentWidth) / 2;
                 }
                 graphics.drawString(content, xx, startHeight + rowHeight * (n + 1) - contentHeight / 2 - contentDescent);
             }
         }
         if (!StringUtils.isEmpty(cellBaseBO.getDesc())){
             graphics.drawString(cellBaseBO.getDesc(), startWidth, startHeight + (totalRow * rowHeight) - 10);
         }
         return image;
     }

    public static void main(String[] args) {

        String tempDir = System.getProperty("java.io.tmpdir") + "temp" + File.separator;
        System.out.println(tempDir);

    }

 }

